﻿using System.Collections.Generic;
using System.Web;

namespace Personify.Modules.UserLocator
{
    /// <summary>
    /// A helper class for managing items in the application cache.
    /// </summary>
    public static class UserLocatorHelper
    {
        private static string USER_LOCATOR_IP_CACHE = "USER_LOCATOR_IP_CACHE";

        /// <summary>
        /// Gets a list of UserLocatorIpInfo objects from the application cache.
        /// </summary>
        /// <returns>The list from the cache.</returns>
        public static List<UserLocatorIpInfo> GetIpData()
        {
            return (List<UserLocatorIpInfo>)HttpContext.Current.Cache.Get(USER_LOCATOR_IP_CACHE);
        }

        /// <summary>
        /// Sets a list of UserLocatorIpInfo objects to the application cache.
        /// </summary>
        /// <param name="items">The list to cache.</param>
        public static void SetIpData(List<UserLocatorIpInfo> items)
        {
            HttpContext.Current.Cache.Insert(USER_LOCATOR_IP_CACHE, items);
        }

        /// <summary>
        /// Gets the JSON data for the provided key from the application cache.
        /// </summary>
        /// <param name="key">The key to retrieve from the cache.</param>
        /// <returns>A UserLocatorJsonInfo object.</returns>
        public static UserLocatorJsonInfo GetJsonData(string key)
        {
            if (HttpContext.Current.Cache.Get(key) == null)
            {
                return new UserLocatorJsonInfo();
            }
            else
            {
                return (UserLocatorJsonInfo)HttpContext.Current.Cache.Get(key);
            }
        }

        /// <summary>
        /// Sets the JSON data to the application cache.
        /// </summary>
        /// <param name="data"></param>
        public static void SetJsonData(UserLocatorJsonInfo data)
        {
            HttpContext.Current.Cache.Insert(data.Token, data);
        }
    }
}
